const config        = require('config');
const Koa           = require('koa');
const convert       = require('koa-convert');
const koaBetterBody = convert(require('koa-better-body')());
const {loge}        = require('./app/utils/logger');
const extend        = require('./app/extend');
const errorHandler  = require('./app/middleware/error-handler');
const routers       = require('./app/middleware/router');

const app = new Koa();

extend(app);

app.use(errorHandler);

/** koa-better-body
 buffer or text      => ctx.request.body
 urlencoded or json  => ctx.request.fields
 multipart           => ctx.request.files
 */
app.use(koaBetterBody);

app.use(routers);

app.listen(config.port, () => {
    console.log(`Now server is listening on port ${config.port} ...`);
});

app.on('error', err => {
    loge(err);
});